package backlet;

import java.util.HashMap;
import java.util.Map;

import login.LoginBacklet;

import test.TestBacklet;
import ui.UIBacklet;
import util.ByteBuffer;
import connect.MessageCode;


/**消息处理逻辑
 * @author Administrator
 *
 */
public class BackletManager
{
	static BackletManager instance;
	
	public  Map<Integer, Backlet> backlets = new HashMap<Integer, Backlet>();

	public BackletManager()
    {
		backlets.put(MessageCode.TEST.BID, new TestBacklet());
		
		backlets.put(MessageCode.LOGIN.BID, new LoginBacklet());
		
		backlets.put(MessageCode.UI.BID, new UIBacklet());
		
    }
	

	public void dealMessage(ByteBuffer data, int sessionId)
    {
		int bid = data.readInt();
		Backlet backlet = backlets.get(bid);
		
		if(backlet == null)
		{
			System.err.println("没有编号为"+bid+"的backlet");
			Thread.dumpStack();
			return;
		}else
		backlet.deal(data,sessionId);
		
    }

	public static BackletManager getInstance()
    {
		if(instance== null)
		{
			instance = new BackletManager();
		}
	    return instance;
    }

}
